﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Configuration;
using Entity.Admin;

namespace Web.Framework.Attributes
{
    /// <summary>
    /// 性能统计属性 .记录Controller Action 的处理速度
    /// </summary>
    public class UserLogsAttribute : ActionFilterAttribute
    {
        private DateTime timeStart = new DateTime();
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            timeStart = DateTime.Now;
            base.OnActionExecuting(filterContext);
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
        }
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            TimeSpan timeSpan = DateTime.Now.Subtract(timeStart);
            filterContext.RouteData.Values.Add("timeSpan", timeSpan);
            base.OnResultExecuted(filterContext);


            string area = filterContext.RouteData.DataTokens["area"] == null ? "" : filterContext.RouteData.DataTokens["area"].ToString();
            string controller = filterContext.RouteData.Values["controller"] == null ? "" : filterContext.RouteData.Values["controller"].ToString();
            string action = filterContext.RouteData.Values["action"] == null ? "" : filterContext.RouteData.Values["action"].ToString();
            UserLog newItem = new UserLog()
            {
                ID = Guid.NewGuid().ToString(),
                Area = area,
                Controller = controller,
                Action = action,
                User = filterContext.HttpContext.User.Identity.Name,
                AddTime = DateTime.Now,
                TimeSpan = timeSpan.TotalSeconds,
                RemoteIP = filterContext.HttpContext.Request.UserHostName,
                Parms = filterContext.HttpContext.Request.Url.Query
            };


            using (DBEntities db = new DBEntities())
            {
                try
                {
                    db.Configuration.AutoDetectChangesEnabled = false;
                    db.Configuration.LazyLoadingEnabled = false;
                    db.Configuration.ProxyCreationEnabled = false;
                    var deleteLog = db.UserLogs.OrderByDescending(p => p.AddTime).Take(2000).FirstOrDefault();
                    db.UserLogs.Add(newItem);
                    int logToDbMax = 2000;
                    if (WebConfigurationManager.AppSettings["LogToDBMax"] != null)
                    {
                        int.TryParse(WebConfigurationManager.AppSettings["LogToDBMax"], out logToDbMax);
                    }
                    if (db.UserLogs.Count() > logToDbMax)
                    {
                        foreach (var item1 in db.UserLogs.OrderByDescending(a => a.AddTime).Skip(logToDbMax))
                        {
                            db.UserLogs.Remove(item1);
                        }
                    }
                    db.SaveChanges();
                }
                catch (Exception ex)
                {

                }

            }
        }
    }
}
